package cn.jyun.user.service.impl;


import cn.jyun.user.mapper.UserMapper;
import cn.jyun.user.pojo.User;
import cn.jyun.user.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public String findAll() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        //查询所有
        String userList = (String) redisTemplate.boundValueOps("user_list").get();
        if (StringUtils.isEmpty(userList)){
            List<User> users = userMapper.selectList(null);
            userList= this.objectMapper.writeValueAsString(users);
            //存入Redis
            redisTemplate.boundValueOps("user_list").set(userList);
            System.out.println("查询数据库。。。");
        }else {
            System.out.println("查询Redis....");
        }
        return userList;
    }


}
